function check_can_like(url_check) {
	var checkOk = true;
	jQuery.ajax({
		type : 'GET',
		url : url_check,
		success : function(result) {
			checkOk = result;
		},
		async : false,
		dataType : 'json'
	});
	return checkOk;
}
	
if (window.location.hash && window.location.hash == '#_=_') {
    if (window.history && history.pushState) {
        window.history.pushState("", document.title, window.location.pathname);
    } else {
        // Prevent scrolling by storing the page's current scroll offset
        var scroll = {
            top: document.body.scrollTop,
            left: document.body.scrollLeft
        };
        window.location.hash = '';
        // Restore the scroll offset, should be flicker free
        document.body.scrollTop = scroll.top;
        document.body.scrollLeft = scroll.left;
    }
}

function readURL(input) {
	if (input.files && input.files[0]){
		var reader = new FileReader();
		reader.onload = function (e) {
			$('.tweet_img_preview')
			.attr('src', e.target.result)
			.fadeIn(400);
		};
	
		reader.readAsDataURL(input.files[0]);
	}
}

function readURL_ie(imgFile)
{    
    $('.tweet_img_preview').attr('src',imgFile.value);
}

function getInternetExplorerVersion() {

	var rv = -1;

	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

$(function(){
	$('.tweet-upload').change(function(){
		var ie = getInternetExplorerVersion();
		
		if(ie <= 9 && ie > 4){
			readURL_ie(this);
		}else{
			readURL(this);
		}
	})
	
	$('.btn-submit-search').click(function(){
		var form_id=$(this).closest('form').attr('id');
		var keyword		=	$('#'+form_id+' .keyword_text').val() ? $('#'+form_id+' .keyword_text').val() : 'search';
		var cat_id		=	$('#'+form_id+' .com_cat_select').val();
		var base_url	=	$('#base_url').val();
		
		var uri			=	base_url+keyword+'-s'+cat_id;
		
		$('#'+form_id).attr('action',uri).submit();
	});
	
	$('.keyword_text').bind("keypress",function(e){
		var form_id=$(this).closest('form').attr('id');
		if(e.keyCode == 13){
			var keyword		=	$('#'+form_id+' .keyword_text').val() ? $('#'+form_id+' .keyword_text').val() : 'search';
			var cat_id		=	$('#'+form_id+' .com_cat_select').val();
			var base_url	=	$('#base_url').val();
			
			var uri			=	base_url+keyword+'-s'+cat_id;
			
			$('#'+form_id).attr('action',uri).submit();
		}
	});
	
	
	$('body').on('click','.cancel-button',function(){
		$.fancybox.cancel();
		$.fancybox.close();
	});
	$('body').on('click','.cancel_user_like',function(){
		
		$.fancybox.cancel();
		$.fancybox.close();
	});
    $('body').on('click','.btn-cancel',function(){
        $.fancybox.cancel();
        $.fancybox.close();
    });

	$('.select_user_like').click(function(){
		check_user();
	});
    $('.select_user_like_all').click(function(){
        check_user();
    });
	
	$('#search_check_all').click(function(){
		
        var $this = $(this);
        var check_flag	=	$this.attr('data');
       
        var url_source	=		$('#base_url_resources').val();
        if(check_flag == 0){
        	$('.select_user_like').prop("checked", true);
        	$this.find('img').attr('src',url_source+'bg_checkbox_active.png');
        	$this.attr('data',1);
        }else{
        	$('.select_user_like').prop("checked", false);
        	$this.find('img').attr('src',url_source+'bg_checkbox.png');
        	$this.attr('data',0);
        }
        
        check_user();
	});
	function check_user(){
		$(".load_content").find('ul').empty();
		
		html	=	'';
		index 	=	0;
		$('.select_user_like').each(function(){
			var user_id	=	$(this).val();
			var image	=	$('#user_image_'+user_id).attr('src');
			
			if($(this).is(":checked")){
				index++;
				html+='<li>';
				html+='<a href="#">';
				html+='<img  src="'+image+'" />';
				html+='<input type="hidden" name="user_id[]" value="'+user_id+'" />';
				html+='</a>';
				html+='</li>';
			}
		});
		
		var message_count	=	'以下の'+index+'人';
		$('.inner-border .count_total').text(message_count);
		$('.inner-border').find('ul').html(html);
	}
	
	$("#search_like_check").click(function () {
		
		var count_user	=	$('.user_like_contain > li').size();
		
		if(!count_user){
			$('.inner-border-empty').show();
			$('.inner-border').hide();
		}else{
			$('.inner-border-empty').hide();
			$('.inner-border').show();
		}
		
		$.fancybox({
	        content: $(".load_content").html(),
	        width:$(".inner-border").width(),
	        height:$(".inner-border").height(),
	        scrolling:'no'
	    });
	});

	$('.submit-tweet').click(function(){
		/*var has_selected_file = $('.tweet-upload').filter(function(){
	        return $.trim(this.value) != ''
	    }).length  > 0 ;

	    if (has_selected_file) {
	    	$('.frm-create-tweet').submit();
	    }else*/
	    	return $('.frm-create-tweet').validationEngine({});
	});
	
	$('.happy_report_to_confirm_btn').click(function(){
		$('.js_violation_form').validationEngine({});
	})
	
	$('.submit-self-intro').click(function(){
		$('.js_violation_form').validationEngine({});
	})
	
	$('.fc_box_block').click(function(){
		$.fancybox({
	        content: $(".block_message").html(),
	        scrolling:'no'
	    });
	})
	
	var temp_w=$('.message').width()+24;
	var margin=($(window).width()-temp_w)/2;
	$('.message').css({'left':margin}).animate({'top':'60px'},1000).fadeIn(400).delay(2000).animate({'top':'-120px'},1000).fadeOut(400);
	
	$('.frm-create').validationEngine({});
    $(".search_cond_tab>li").on('click',function(){
        $(".search_cond_tab>li.active").removeClass('active');
        $(this).addClass('active');
        index = $(this).index();
        $(".search_cond_tab_contents>div").addClass('hide');
        $(".search_cond_tab_contents>div:eq("+index+")").removeClass('hide');

    });
});